JavaFX 您所在的位置:网站首页 javafx 设计器 JavaFX

JavaFX

2023-10-19 06:25| 来源: 网络整理| 查看: 265

目录

JavaFX的基本结构和概念

Application --- 代表了JavaFX的应用程序

Stage --- 舞台 --- 代表了一个窗体。

在Application的start方法中,提供了一个默认的窗体对象。我们也可以根据自己的需要,new出新的窗体。

Scene --- 场景 --- 代表了窗体当中的内容板。 Scene对象在创建的时候要求传参,参数是一个布局。

JavaFX控件

标签 Label

文本框 TextField

密码框 PasswordField

下拉框 ComboBox

按钮 Button

单选按钮 RadioButton

复选框 CheckBox

JavaFX的基本结构和概念 Application --- 代表了JavaFX的应用程序 Stage --- 舞台 --- 代表了一个窗体。 在Application的start方法中,提供了一个默认的窗体对象。我们也可以根据自己的需要,new出新的窗体。

示例:

public class MyApplication extends Application { public static void main(String[] args) { launch(); } @Override public void start(Stage stage) throws Exception { stage.setScene(new LoginScene);//设置运行时场景 stage.setTitle("第一个窗体");//设置窗体标题 stage.getIcons().add(new Image("file:img/logo.gif"));//设置窗体的logo //如果不写的话,以Stage中的Scene的大小决定 // stage.setWidth(600);//设置窗体的宽度 // stage.setHeight(400);//设置窗体的高度 //不写的话默认居中 // stage.setX(100);//定制距离父容器左上角的x坐标 // stage.setY(100);//定制距离父容器左上角的y坐标 stage.setResizable(false);//设置窗体大小不可变 stage.show();//最好放在最后一条语句,保证内容构造好以后再一次性显示 } } Scene --- 场景 --- 代表了窗体当中的内容板。 Scene对象在创建的时候要求传参,参数是一个布局。

锚点布局面板 示例:

public class LoginScene extends Scene { private AnchorPane anchorPane; public LoginScene(){ //锚点布局面板 --- 采用绝对定位的方式布局它内部的空间或子面板的位置 super(new AnchorPane()); this.anchorPane = (AnchorPane) this.getRoot(); } } JavaFX控件 标签 Label

示例:

Label nameLab = new Label("用户名:");//设置标签文本 this.nameLab.setLayoutX(30);//设置x轴绝对定位 this.nameLab.setLayoutY(30);//设置y轴绝对定位 this.nameLab.setFont(new Font("宋体",18));//设置字体和文字大小 or .setStyle方法 this.anchorPane.getChildren().add(this.nameLab);//添加该控件 文本框 TextField

示例:

TextField nameTxt = new TextField(); this.nameTxt.setLayoutX(100); this.nameTxt.setLayoutY(24); this.nameTxt.setWidth(100);//设置文本框宽 this.nameTxt.setHeight(30);//设置文本框高 this.nameTxt.setPromptText("请输入姓名");//设置提示语句,文本框获取焦点以后自动消失 this.nameTxt.setFont(new Font("宋体",15)); this.anchorPane.getChildren().add(this.nameTxt); 密码框 PasswordField

示例:

PasswordField pwdTxt = new PasswordField(); this.pwdTxt.setLayoutX(100); this.pwdTxt.setLayoutY(75); this.pwdTxt.setFont(new Font("宋体",15)); this.anchorPane.getChildren().add(this.pwdTxt); 下拉框 ComboBox

示例:

ComboBox idComb = new ComboBox(); this.idComb.getItems().add("管理员");//添加可选项 this.idComb.getItems().add("会员"); this.idComb.getItems().add("非会员"); this.idComb.setStyle("-fx-font-size:15px");//修改字体大小 this.idComb.setLayoutX(100); this.idComb.setLayoutY(124); this.idComb.getSelectionModel().select(1);//修改默认显示选项 this.anchorPane.getChildren().add(this.idComb); 按钮 Button

示例:

Button loginBtn = new Button("登陆"); this.loginBtn.setFont(new Font("宋体",18)); this.loginBtn.setLayoutX(300); this.loginBtn.setLayoutY(180); this.anchorPane.getChildren().add(this.loginBtn); 单选按钮 RadioButton

示例:

//单选按钮 RadioButton maleButton = new RadioButton("男"); RadioButton femalButton = new RadioButton("女"); RadioButton unknownButton = new RadioButton("未知"); this.maleButton.setLayoutX(20); this.maleButton.setLayoutY(200); this.femalButton.setLayoutX(120); this.femalButton.setLayoutY(200); this.unknownButton.setLayoutX(220); this.unknownButton.setLayoutY(200); this.unknownButton.setSelected(true);//设置默认选中 //要有互斥效果,必须先分组 ToggleGroup genderGroup = new ToggleGroup(); this.maleButton.setToggleGroup(genderGroup); this.femalButton.setToggleGroup(genderGroup); this.unknownButton.setToggleGroup(genderGroup); 复选框 CheckBox

示例:

//复选框 CheckBox readBox = new CheckBox("阅读"); CheckBox musicBox = new CheckBox("运动"); CheckBox sportBox = new CheckBox("音乐"); CheckBox travelBox = new CheckBox("旅游"); this.readBox.setLayoutX(20); this.readBox.setLayoutY(240); this.sportBox.setLayoutX(100); this.sportBox.setLayoutY(240); this.musicBox.setLayoutX(180); this.musicBox.setLayoutY(240); this.travelBox.setLayoutX(260); this.travelBox.setLayoutY(240); this.anchorPane.getChildren().addAll(this.readBox,this.sportBox,this.musicBox,this.travelBox);


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有